package tech.aistar.corejava.day05.homework03;

import java.util.Arrays;

/**
 * @author success
 * @version 1.0
 * @description:本类用来演示:
 * @date 2019/7/23 0023
 */
public class ProductBiz {

    public Product[] getByPrice(Product[] products,double price){
        //1.参数有效性判断
        if(null == products || products.length == 0){
            return null;
        }

        //2. 超过price的放入到新的数组中
        //定义一个计数器
        int count = 0;
        //找出超过price的产品的个数
        for (int i = 0; i < products.length; i++) {
            if(products[i].getPrice()>300.0){
                count++;
            }
        }

        //确定一个新的数组
        Product[] pts = new Product[count];

        //定义一个下标计数器
        int pos = 0;

        for (int i = 0; i < products.length; i++) {
            if(products[i].getPrice()>price){
                pts[pos++] = products[i];
            }
        }
        return pts;
    }

    public Product[] sort(Product[] products){
        //又不希望对原来的数组进行改变
        Product[] pts = Arrays.copyOf(products,products.length);

        //2. 价格从高到低
        //选择算法

        for (int i = 0; i < pts.length; i++) {
            for (int j = i+1; j < pts.length; j++) {
                if(pts[i].getPrice()<pts[j].getPrice()){
                    //交换俩个对象.
                    Product temp = pts[i];
                    pts[i] = pts[j];
                    pts[j] = temp;
                }
            }
        }

        return pts;
    }
}
